package pc

import (
	. "chess_platform/internal/common"
	"fmt"
	"github.com/astaxie/beego/orm"
)

//添加奖品
func NewLotteryProduct(data map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChess)
	values, sql := InsertSql(TableLotteryProduct, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//更新奖品
func UpdateLotteryProduct(data map[string]interface{}, id int64) (int64, error) {
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TableLotteryProduct, data, condition)
	o := NewDBOrm(DBChess)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

//抽奖的奖品列表
func ListLotteryProduct(p ListParams, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		LIMIT ?,?
		`, TableLotteryProduct)
	o := NewOrm(multiOrm, DBChess)
	var gl []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&gl)
	if CheckError(err) {
		return gl, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `,
		TableLotteryProduct)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return gl, count, err
	}
	return gl, count, nil
}

//玩家拿钻石兑换抽奖次数的记录列表
func ListLotteryConsume(p ListParams, uid int64, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	where := fmt.Sprintf(`uid = %v`, uid)
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			%v
		LIMIT ?,?
		`, TableLotteryConsumeRecord, where)
	o := NewOrm(multiOrm, DBChess)
	var gl []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&gl)
	if CheckError(err) {
		return gl, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v WHERE %v `,
		TableLotteryConsumeRecord, where)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return gl, count, err
	}
	return gl, count, nil
}

//----------------------exchange------------------------------
func NewLotteryExchange(data map[string]interface{}, multiOrm ...orm.Ormer) (int64, error) {
	o := NewOrm(multiOrm, DBChess)
	values, sql := InsertSql(TableLotteryExchange, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

func UpdateLotteryExchange(data map[string]interface{}, id int64) (int64, error) {
	condition := fmt.Sprintf(`id = %v`, id)
	values, sql := UpdateSql(TableLotteryExchange, data, condition)
	o := NewDBOrm(DBChess)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

func LotteryExchangeById(id int64, multiOrm ...orm.Ormer) (LotteryExchange, error) {
	o := NewOrm(multiOrm, DBChess)
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			id = %v
	`, TableLotteryExchange, id)
	var ex LotteryExchange
	err := o.Raw(sql).QueryRow(&ex)
	return ex, err
}

func DeleteLotteryExchange(id int64) (int64, error) {
	o := NewDBOrm(DBChess)
	condition := fmt.Sprintf("id=%v", id)
	sql := DeleteSql(TableLotteryExchange, condition)
	result, err := o.Raw(sql).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

func ListLotteryExchange(p ListParams, multiOrm ...orm.Ormer) ([]orm.Params, int64, error) {
	limit := (p.PageIndex - 1) * p.PageSize
	sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		LIMIT ?,?
		`, TableLotteryExchange)
	o := NewOrm(multiOrm, DBChess)
	var gl []orm.Params
	_, err := o.Raw(sql, limit, p.PageSize).QueryRows(&gl)
	if CheckError(err) {
		return gl, 0, err
	}
	sql = fmt.Sprintf(`SELECT COUNT(1) FROM %v `,
		TableLotteryExchange)
	var count int64
	err = o.Raw(sql).QueryRow(&count)
	if CheckError(err) {
		return gl, count, err
	}
	return gl, count, nil
}
